package cn.stylefeng.guns.modular.milestoneDemand.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.milestoneDemand.entity.MilestoneDemand;
import cn.stylefeng.guns.modular.milestoneDemand.mapper.MilestoneDemandMapper;
import cn.stylefeng.guns.modular.milestoneDemand.model.params.MilestoneDemandParam;
import cn.stylefeng.guns.modular.milestoneDemand.model.result.MilestoneDemandResult;
import  cn.stylefeng.guns.modular.milestoneDemand.service.MilestoneDemandService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 里程碑-需求关联表 服务实现类
 * </p>
 *
 * @author jalor
 * @since 2019-10-20
 */
@Service
public class MilestoneDemandServiceImpl extends ServiceImpl<MilestoneDemandMapper, MilestoneDemand> implements MilestoneDemandService {

    @Override
    public void add(MilestoneDemandParam param){
        MilestoneDemand entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MilestoneDemandParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(MilestoneDemandParam param){
        MilestoneDemand oldEntity = getOldEntity(param);
        MilestoneDemand newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MilestoneDemandResult findBySpec(MilestoneDemandParam param){
        return null;
    }

    @Override
    public List<MilestoneDemandResult> findListBySpec(MilestoneDemandParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MilestoneDemandParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MilestoneDemandParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MilestoneDemand getOldEntity(MilestoneDemandParam param) {
        return this.getById(getKey(param));
    }

    private MilestoneDemand getEntity(MilestoneDemandParam param) {
        MilestoneDemand entity = new MilestoneDemand();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
